package io.github.wx.core.config;

import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.template.Engine;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import io.github.wx.common.shiro.tag.*;
import io.github.wx.core.plugin.shiro.ShiroInterceptor;
import io.github.wx.core.plugin.shiro.ShiroPlugin;
import io.github.wx.core.route.AdminRouts;
import io.github.wx.core.route.WeiXinRoutes;
import net.dreamlu.ui.jfinal.AssetsDirective;

/**
 * Created by jie on 2017/4/1.
 * 框架的核心配置
 */
public class LmsCoreConfig extends JFinalConfig {
    private static Prop p = loadConfig();

    private static Prop loadConfig() {
        try {
            return PropKit.use("profile.dev.properties");
        } catch (Exception e) {
            return PropKit.use("profile.sit.properties");
        }
    }

    /**
     * 配置全局常量
     *
     * @param me Constants
     */
    public void configConstant(Constants me) {
        me.setDevMode(p.getBoolean("devMode", false));
        ApiConfigKit.setDevMode(p.getBoolean("devMode", false));
        me.setJsonFactory(new FastJsonFactory());
    }

    /**
     * 配置路由
     *
     * @param me Routes
     */
    public void configRoute(Routes me) {
        me.add(new WeiXinRoutes());
        me.add(new AdminRouts());
    }

    /**
     * 配置模板引擎
     *
     * @param me Engine
     */
    public void configEngine(Engine me) {
        /*shiroTag*/
        me.addDirective("authenticated", new AuthenticatedTag());
        me.addDirective("guest", new GuestTag());
        me.addDirective("hasAllPermission", new HasAllPermissionTag());
        me.addDirective("hasAnyRoles", new HasAnyRolesTag());
        me.addDirective("hasPermission", new HasPermissionTag());
        me.addDirective("hasRole", new HasRoleTag());
        me.addDirective("lacksPermission", new LacksPermissionTag());
        me.addDirective("lacksRole", new LacksRoleTag());
        me.addDirective("noAuthenticated", new NoAuthenticatedTag());
        me.addDirective("principal", new PrincipalTag());
        me.addDirective("shiroUser", new ShiroUserTag());
        /*js、css压缩*/
        me.addDirective("assets", new AssetsDirective());
        me.addSharedFunction("_common/common.html");
    }

    /**
     * 配置插件
     *
     * @param me Plugins
     */
    public void configPlugin(Plugins me) {
        RedisPlugin redisPlugin = new RedisPlugin(p.get("redisCacheName"), p.get("redisHost"), p.get("redisPwd"));
        me.add(redisPlugin);
        me.add(new ShiroPlugin());
    }

    /**
     * 配置拦截器
     *
     * @param me Interceptors
     */
    public void configInterceptor(Interceptors me) {
        me.add(new ShiroInterceptor());
    }

    /**
     * 配置自定义处理器
     *
     * @param me Handlers
     */
    public void configHandler(Handlers me) {
        me.add(new ContextPathHandler("ctx"));
    }

    /**
     * 程序启动入口
     * 特别注意：IDEA 之下建议的启动方式，仅比 eclipse 之下少了最后一个参数
     *
     * @param args Args
     */
    public static void main(String[] args) {
        JFinal.start("web/src/main/webapp", 80, "/");
    }
}
